from fastapi import HTTPException
from peewee import *
from ..dependencies import success, router, depens, me
from ..models.info import Help, Article, Feedback
from pydantic import BaseModel, Field

class HelpForm(BaseModel):
    consignee: str = Field(title="收货人", default='', max_length=20)
    mobile: str = Field(title="手机号", default='', max_length=11)
    dist_id: int = Field(title="地区", default=0)
    detail: str = Field(title="详细", default='', max_length=50)

@router.get('/help', tags=['info'])
async def index(page: int = 1, size: int = 10):
    list = [row for row in Help.select().order_by(Help.id).paginate(page, size).dicts()]
    return success({
        'list': list,
        'total': Help.select().count()
    })

@router.get('/help/{id}', tags=['info'])
async def read(id: int):
    model = Help.get(Help.id == id)
    if model == None:
        raise HTTPException(status_code=404, detail="Help not found")
    return success(model.__data__)

@router.put('/help/{id}', tags=['info'])
async def update(id: int, params):
    model = Help.update(params).where(Help.id == id).execute()
    return success(model.__data__)
############
### 文章 ###
############
class ArticleForm(BaseModel):
    consignee: str = Field(title="收货人", default='', max_length=20)
    mobile: str = Field(title="手机号", default='', max_length=11)
    dist_id: int = Field(title="地区", default=0)
    detail: str = Field(title="详细", default='', max_length=50)

@router.get('/article', tags=['info'])
async def index(page: int = 1, size: int = 10):
    list = [row for row in Article.select().order_by(Article.id).paginate(page, size).dicts()]
    return success({
        'list': list,
        'total': Article.select().count()
    })

@router.get('/article/{id}', tags=['info'])
async def read(id: int):
    model = Article.get(Article.id == id)
    if model == None:
        raise HTTPException(status_code=404, detail="Help not found")
    return success(model.__data__)
### 创建
@router.post('/article', tags=["order"], dependencies=depens())
async def save(post: ArticleForm):
    model = Article.create(user_id=me('id'), **post.__dict__)
    return success(model.__data__)
### 更新
@router.put('/article/{id}', tags=["order"], dependencies=depens())
async def update(id: int, model: ArticleForm):
    model = Article.update(model.__dict__).where(Article.id == id).execute()
    return success()
### 删除
@router.delete('/article/{id}', tags=["order"], dependencies=depens())
async def delete(id: int):
    Article.delete().where(Article.id==id, Article.user_id==me('id')).execute()
    return success()
############
### 反馈 ###
############
class FeedbackForm(BaseModel):
    consignee: str = Field(title="收货人", default='', max_length=20)
    mobile: str = Field(title="手机号", default='', max_length=11)
    dist_id: int = Field(title="地区", default=0)
    detail: str = Field(title="详细", default='', max_length=50)

@router.get('/feedback', tags=['info'])
async def index(page: int = 1, size: int = 10):
    list = [row for row in Article.select().order_by(Article.id).paginate(page, size).dicts()]
    return success({
        'list': list,
        'total': Feedback.select().count()
    })

@router.get('/feedback/{id}', tags=['info'])
async def read(id: int):
    model = Feedback.get(Feedback.id == id)
    if model == None:
        raise HTTPException(status_code=404, detail="Help not found")
    return success(model.__data__)
### 创建
@router.post('/feedback', tags=["order"], dependencies=depens())
async def save(post: FeedbackForm):
    model = Feedback.create(user_id=me('id'), **post.__dict__)
    return success(model.__data__)
### 更新
@router.put('/feedback/{id}', tags=["order"], dependencies=depens())
async def update(id: int, model: FeedbackForm):
    model = Feedback.update(model.__dict__).where(Feedback.id == id).execute()
    return success()
### 删除
@router.delete('/feedback/{id}', tags=["order"], dependencies=depens())
async def delete(id: int):
    Feedback.delete().where(Feedback.id==id, Feedback.user_id==me('id')).execute()
    return success()